using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace TmxUtil
{
    public class ResXCollection : Collection<ResX>
    {
        public ResXCollection()
        {
        }

        public ResXCollection(IEnumerable<ResX> list)
        {
            foreach (ResX item in list)
                Add(item);
        }

        public void AddRange(IEnumerable<ResX> list)
        {
            foreach (ResX item in list)
                Add(item);
        }

        public void Fix()
        {
            Dictionary<string, object> kh = new Dictionary<string, object>();
            foreach (ResX rx in this)
                if (rx.CultureName == string.Empty)
                    kh[rx.Key] = null;

            List<ResX> filtered = new List<ResX>();
            foreach(ResX rx in this)
            {
                if (kh.ContainsKey(rx.Key))
                    filtered.Add(rx);
            }

            Items.Clear();
            foreach(ResX rx in filtered)
                Items.Add(rx);
        }


        public string[] GetKeys()
        {
            Dictionary<string, ResX> d = new Dictionary<string, ResX>();
            foreach (ResX item in this)
                d[item.Key] = item;

            string[] ret = new string[d.Count];
            d.Keys.CopyTo(ret, 0);
            Array.Sort(ret);
            return ret;
        }

        public string[] GetFiles()
        {
            List<string> ret = new List<string>();
            foreach (ResX x in this)
                ret.Add(x.FullName);

            return ret.ToArray();
        }

        public ResX[] Find(string resKey)
        {
            List<ResX> list = new List<ResX>();
            foreach (ResX item in this)
                if (item.Key == resKey)
                    list.Add(item);

            return list.ToArray();
        }

        public ResX[] FindByCulture(string cultureName)
        {
            List<ResX> list = new List<ResX>();
            foreach (ResX item in this)
                if (item.CultureName == cultureName)
                    list.Add(item);

            list.Sort(delegate(ResX x, ResX y) { return x.File.CompareTo(y.File); });
            return list.ToArray();
        }

        public string[] GetCultures()
        {
            Dictionary<string, ResX> d = new Dictionary<string, ResX>();
            foreach (ResX item in this)
                d[item.CultureName] = item;

            string[] ret = new string[d.Count];
            d.Keys.CopyTo(ret, 0);
            return ret;
        }

        public string[] GetCultures(string key)
        {
            Dictionary<string, ResX> d = new Dictionary<string, ResX>();
            foreach (ResX item in this)
                if (item.Key == key)
                    d[item.CultureName] = item;

            string[] ret = new string[d.Count];
            d.Keys.CopyTo(ret, 0);
            return ret;
        }

        public void Sort()
        {
            List<ResX> copy = new List<ResX>(this);
            copy.Sort(delegate(ResX x, ResX y) { return x.File.CompareTo(y.File); });

            Items.Clear();
            foreach(ResX item in copy)
                Items.Add(item);
        }
    }
}